﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.CRM.Component.Common;
using TRPOP.CRM.Component.Business;
using TRPOP.CRM.Component.Model;
using TRPOP.Common;

namespace TRPOP.CRM.Web
{
    /// <summary>
    /// 更新 新增部门信息
    /// </summary>
    public partial class Department_save : BaseAdmin
    {
        #region 权限限制
        protected override void SubPageInit()
        {
            base.m_oCurrentPermissionKey = "Sys_Department";
        }
        #endregion

        #region 页面变量
        private DepartmentInfo m_oDepartmentInfo = new DepartmentInfo();
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
                if (RequestHelper.RequestInt("id") > 0)
                {
                    BindData();
                }
            }
        }

        //修改 新增 Department
        public void BtnSubmit_Click(object sender, EventArgs e)
        {
            if (ViewState["Department"] != null)
            {
                //修改
                m_oDepartmentInfo = ViewState["Department"] as DepartmentInfo;
            }
            else
            {
                //新增
                m_oDepartmentInfo.dtCreationTime = DateTime.Now;
                m_oDepartmentInfo.bIsDelete = false;
            }

            m_oDepartmentInfo.nvcDepartmentCode = txtDepartmentCode.Text.HtmlEncode();
            m_oDepartmentInfo.nvcDepartmentName = txtDepartmentName.Text.HtmlEncode();
            m_oDepartmentInfo.iParentDepartmentID = ddlParentDepartment.SelectedValue.ToInt();

            //保存
            if (DepartmentManage.SaveDepartment(m_oDepartmentInfo))
            {
                Alert("保存成功！", "Department_list.aspx");
            }
            else
            {
                Alert("保存失败！");
            }
        }

        #endregion

        #region 页面方法
        protected void BindData()
        {
            m_oDepartmentInfo = DepartmentManage.GetDepartmentById(RequestHelper.RequestInt("id"));

            if (m_oDepartmentInfo != null)
            {
                ViewState["Department"] = m_oDepartmentInfo;
                txtDepartmentCode.Text = m_oDepartmentInfo.nvcDepartmentCode.HtmlDecode();
                txtDepartmentName.Text = m_oDepartmentInfo.nvcDepartmentName.HtmlDecode();

                //当前判断是否是顶级单位
                if (m_oDepartmentInfo.iParentDepartmentID == 0)
                {
                    //当前已经是顶级 则直接绑定
                    ddlParentDepartment.Items.Clear();
                    ddlParentDepartment.Items.Insert(0, new ListItem("顶级单位", "0"));
                }
                else
                {
                    ddlParentDepartment.SelectedValue = m_oDepartmentInfo.iParentDepartmentID.ToString();
                }
            }
        }

        protected void LoadBase()
        {
            PageBindHelper.BindDepartmentInfo(ddlParentDepartment, RequestHelper.RequestInt("id"));
        }
        #endregion
    }
}